package com.ehualu.getes.until;

import org.elasticsearch.client.RestHighLevelClient;

import java.awt.geom.Point2D;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: 吴敬超
 * @Date: 2019/11/15 18:16
 */
public class gcuse {


    private static String host = "37.158.97.130,37.158.97.131,37.158.97.132";
    private static int port = 24148;
    private static String schema = "http";

    public Double gcsd2(String index, String[] kkid1, String[] kkid2) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, -10);

//        cal.add(Calendar.MINUTE, -30);
        Date stardate = cal.getTime();

        String starttime = simpleDateFormat.format(stardate);
        System.out.println("****starttime*****：" + starttime);

        Calendar calend = Calendar.getInstance();
        calend.add(Calendar.HOUR, -8);

//        calend.add(Calendar.MINUTE, -30);
        Date enddate = calend.getTime();

        String endtime = simpleDateFormat.format(enddate);
        System.out.println("****endtime*****：" + endtime);


        String starttime1 = "2019-11-12T10:53:02.000Z";
        String endtime1 = "2019-11-12T11:53:02.000Z";
        List<Double> gcsdlist = new ArrayList<>();


        ESClientbui esClientbui = new ESClientbui();
        RestHighLevelClient client = esClientbui.getHighLevelClient(host, port, schema);


        ESsdUntil eSsdUntil = new ESsdUntil();

        List<String[]> kk1list = eSsdUntil.getgcjl(starttime1, endtime1, index, kkid1, client);

        List<String[]> kk2list = eSsdUntil.getgcjl(starttime1, endtime1, index, kkid2, client);

        esClientbui.close(client);


        if (kk1list.size() == 0) {

            System.out.println("卡口近30分钟没有过车，卡口id：" + kkid1);

            return 0.0;
        } else if (kk2list.size() == 0) {

            System.out.println("卡口近30分钟没有过车，卡口id：" + kkid2);
            return 0.0;

        }

        double kk1jd = Double.valueOf(kk1list.get(0)[2]);
        double kk1wd = Double.valueOf(kk1list.get(0)[3]);

        double kk2jd = Double.valueOf(kk2list.get(0)[2]);
        double kk2wd = Double.valueOf(kk2list.get(0)[3]);

        Point2D point1 = new Point2D.Double(kk1jd, kk1wd);

        Point2D point2 = new Point2D.Double(kk2jd, kk2wd);

//        两点间米数
        double dsize = Dissize.getDistance(point1, point2);

        System.out.println("********ffffffkk1listffff***********");
//
//        for (int i = 0; i < kk1list.size(); i++) {
//
////          卡口1的车牌
//            String kk1cph = kk1list.get(i)[0];
////           卡口1过车时间
//            String kk1gcsj = kk1list.get(i)[1];
//
//
//            System.out.println("kk1cph:"+kk1cph);
////卡口2循环遍历
//
//
//        }
//
//        System.out.println("********ffffffkk2listffff***********");
//
//
//        for (int y = 0; y < kk2list.size(); y++) {
//            String kk2cph = kk2list.get(y)[0];
//            String kk2gcsj = kk2list.get(y)[1];
//            System.out.println("kk2cph:"+kk2cph);
//
//
//        }
//        System.out.println("********ffffffffff***********");


//        循环遍历卡口1的数据
       /* for (int i = 0; i < kk1list.size(); i++) {



            //          卡口1的车牌
            String kk1cph = kk1list.get(i)[0];
//           卡口1过车时间
            String kk1gcsj = kk1list.get(i)[1];

//卡口2循环遍历
            for (int y = 0; y < kk2list.size(); y++) {




                String kk2cph = kk2list.get(y)[0];
                String kk2gcsj = kk2list.get(y)[1];


//                System.out.println("kk2cph:"+kk2cph);
//                System.out.println("kk2gcsj:"+kk2gcsj);
                if (kk2cph.equals(kk1cph)) {

                    System.out.println("找到相同的车牌:" + kk2cph);
//
//
//                    Date firstdate = null;
//                    Date lastdate = null;
//                    try {
//                        firstdate = simpleDateFormat.parse(kk1gcsj);
//                        lastdate = simpleDateFormat.parse(kk2gcsj);
//                    } catch (ParseException e) {
//                        e.printStackTrace();
//                    }
//                    long firstdatelong = firstdate.getTime();
//                    long lastdatelong = lastdate.getTime();
//                    long stimecz = ((firstdatelong - lastdatelong) / (1000));
//
//
//                    System.out.println("两个时间之间的秒差为：" + stimecz);
//
//
//                    long absstimecz = Math.abs(stimecz);
//
//                    System.out.println("两个时间之间的秒差绝对值为：" + absstimecz);
//                    double gcsdone = dsize / absstimecz;
//
//                    System.out.println("当前过车速度：" + gcsdone);
//
//                    gcsdlist.add(gcsdone);
//
//                    break;
                }


            }


        }
*/

        System.out.println("gcsdlist.size：" + gcsdlist.size());

        double sum = 0;
        for (int n = 0; n < gcsdlist.size(); n++) {

            sum = sum + gcsdlist.get(n);

        }


        double pjsd = sum / gcsdlist.size();

        System.out.println("平均过车速度：" + pjsd);


        return pjsd;
    }

    public Double gcsd(String index, String[] kkid1, String[] kkid2) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, -8);

        cal.add(Calendar.MINUTE, -10);
        Date stardate = cal.getTime();

        String starttime = simpleDateFormat.format(stardate);
        System.out.println("****starttime*****：" + starttime);

        Calendar calend = Calendar.getInstance();
        calend.add(Calendar.HOUR, -8);

//        calend.add(Calendar.MINUTE, -30);
        Date enddate = calend.getTime();

        String endtime = simpleDateFormat.format(enddate);
        System.out.println("****endtime*****：" + endtime);


        List<Double> gcsdlist = new ArrayList<>();


        ESClientbui esClientbui = new ESClientbui();
        RestHighLevelClient client = esClientbui.getHighLevelClient(host, port, schema);


        ESsdUntil eSsdUntil = new ESsdUntil();

        List<String[]> kk1list = eSsdUntil.getgcjl(starttime, endtime, index, kkid1, client);

        List<String[]> kk2list = eSsdUntil.getgcjl(starttime, endtime, index, kkid2, client);

        esClientbui.close(client);


        if (kk1list.size() == 0) {

            System.out.println("卡口近30分钟没有过车，卡口id：" + kkid1);

            return 0.0;
        } else if (kk2list.size() == 0) {

            System.out.println("卡口近30分钟没有过车，卡口id：" + kkid2);
            return 0.0;

        }

        double kk1jd = Double.valueOf(kk1list.get(0)[2]);
        double kk1wd = Double.valueOf(kk1list.get(0)[3]);

        double kk2jd = Double.valueOf(kk2list.get(0)[2]);
        double kk2wd = Double.valueOf(kk2list.get(0)[3]);

        Point2D point1 = new Point2D.Double(kk1jd, kk1wd);

        Point2D point2 = new Point2D.Double(kk2jd, kk2wd);

//        两点间米数
        double dsize = Dissize.getDistance(point1, point2);


        System.out.println("两点间米数:" + dsize);

//        循环遍历卡口1的数据
        for (int i = 0; i < kk1list.size(); i++) {

//          卡口1的车牌
            String kk1cph = kk1list.get(i)[0];
//           卡口1过车时间
            String kk1gcsj = kk1list.get(i)[1];

//卡口2循环遍历
            for (int y = 0; y < kk2list.size(); y++) {
                String kk2cph = kk2list.get(y)[0];
                String kk2gcsj = kk2list.get(y)[1];


                if (kk2cph.equals(kk1cph)) {

                    System.out.println("找到相同的车牌:" + kk2cph);


                    Date firstdate = null;
                    Date lastdate = null;
                    try {
                        firstdate = simpleDateFormat.parse(kk1gcsj);
                        lastdate = simpleDateFormat.parse(kk2gcsj);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    long firstdatelong = firstdate.getTime();
                    long lastdatelong = lastdate.getTime();
                    long stimecz = ((firstdatelong - lastdatelong) / (1000));


                    System.out.println("两个时间之间的秒差为：" + stimecz);


                    long absstimecz = Math.abs(stimecz);

                    System.out.println("两个时间之间的秒差绝对值为：" + absstimecz);
                    double gcsdone = dsize / absstimecz;

                    System.out.println("当前过车速度：" + gcsdone);

                    gcsdlist.add(gcsdone);

                    break;
                }


            }


        }

        double sum = 0;
        for (int n = 0; n < gcsdlist.size(); n++) {

            sum = sum + gcsdlist.get(n);

        }


        double pjsd = sum / gcsdlist.size();

        System.out.println("平均过车速度：" + pjsd);


        return pjsd;
    }


/*
    public static void main(String[] args) {

        gcuse gc = new gcuse();
//        gc.gcsd("guoche_201911", "3715010110989", "3715010112170");
//        gc.gcsd("guoche_201911", "3715010112170", "3715010110989");

    }
*/
}
